from requests import post, get
from const import PATH
from os.path import join
from login import login 
import traceback

USERNAME = "wuweim6205" 
PASSWORD = "112233"

r = login(USERNAME,PASSWORD)
if not r.json()['err']:
    cookies = r.cookies
else:
    raise

URL = "http://www.jisilu.cn/inbox/ajax/send/"
URL_RM = "http://www.jisilu.cn/inbox/delete_dialog/dialog_id-%s"

def rm():
    html = get("http://www.jisilu.cn/inbox/", cookies=r.cookies).content.decode("utf-8")
    print(html)

rm()
raise
def _send(username):
    r = post(
        URL,
        dict(
            recipient = username, 
            message = """%s , 您好:

我最近在编著一本面向青年人的投资入门指南，
希望能邀请到更多从事投资的朋友共同来撰写。

目前已写完了一些样篇，如

    大空头 - 约翰・保尔森 : 
        创造了华尔街历史上最高个人年收入记录的男人
    http://tz.world/book-018

不知道您有没有兴趣？(有兴趣可以点击书页右上角的『我想参与』)

另外，也欢迎关注书的微信公众号（见文章页底部）。
        """%username,
            _post_type = "ajax"
        ),
        cookies = cookies,
        timeout = 30
    )
    response = r.json()
    print(username,  response)
    return response 

def send(username):
    for i in range(3):
        try:
            return _send(username)
        except:
            traceback.print_exc()

def main():
    sended_log = join(PATH,"jisilu_sended.txt")
    with open(sended_log , "r") as f:
        sended = set(i.strip() for i in f)

    count = 0
    with open(join(PATH,"user_list.txt")) as user_list:
        with open(sended_log , "a") as f:
            for username in reversed(list(user_list)):
                username = username.strip()
                if username in sended:
                    print("pass", username)
                    continue
                if username:
                    if send(username):
                        print(count)
                        count += 1
                        f.write(username+"\n")
                        f.flush()

main()


